﻿using BNS_Education.IBLL;
using BNS_Education.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BNS_Education.UI.Controllers
{
    public class PaperController : Controller
    {
        //父级试卷
        private Itestpaper testpaperUI = BNS_Education.BLLContainer.Container.Resolve<Itestpaper>();
        //子试卷
        private ISubvolumesbll ISubvolumesbll = BNS_Education.BLLContainer.Container.Resolve<ISubvolumesbll>();
        // GET: Paper
        public ActionResult Index(string shijuan = "")
        {
            var list = testpaperUI.GetModels(p => true);
            var list1 = ISubvolumesbll.GetModels(p => true);
            if (!string.IsNullOrWhiteSpace(shijuan))
            {

                var list3 = from a in list.ToList()
                            join b in list1.ToList()
on a.testpaperId equals (Convert.ToInt32(b.topicId))
                            where a.testpaperName.Contains(shijuan)
                            select new KJKHKH()
                            {
                                SubvolumesId = b.SubvolumesId,
                                SubvolumesName = b.SubvolumesName,
                                SubvolumesShiJian = b.SubvolumesShiJian,
                                Subvolumesshitiluanxu = b.Subvolumesshitiluanxu,
                                Subvolumesxuanxiangluanxu = b.Subvolumesxuanxiangluanxu,
                                testpaperBeiZhu = a.testpaperBeiZhu,
                                testpaperId = a.testpaperId,
                                testpaperName = a.testpaperName,
                                topicId = a.testpaperId.ToString()
                            };

                //var result = list.Where(p => p.testpaperName.Contains(shijuan)).ToList();

                ViewBag.aaa = list3;
                return View();
            }
            else
            {
                var list3 = from a in list.ToList()
                            join b in list1.ToList()
on a.testpaperId equals (Convert.ToInt32(b.topicId))
                            select new KJKHKH()
                            {

                                SubvolumesId = b.SubvolumesId,
                                SubvolumesName = b.SubvolumesName,
                                SubvolumesShiJian = b.SubvolumesShiJian,
                                Subvolumesshitiluanxu = b.Subvolumesshitiluanxu,
                                Subvolumesxuanxiangluanxu = b.Subvolumesxuanxiangluanxu,
                                testpaperBeiZhu = a.testpaperBeiZhu,
                                testpaperId = a.testpaperId,
                                testpaperName = a.testpaperName,
                                topicId = a.testpaperId.ToString()
                            };
                ViewBag.aaa = list3;
                return View();
            }


        }
        public ActionResult shanchu(int id)
        {
            Subvolumes m = new Subvolumes();
            m.SubvolumesId = id;
            if(ISubvolumesbll.Delete(m))
            {
                return Content("<script>alert('删除成功');location.href='/Paper/index'</script>");
            }
            else
            {
                return Content("<script>alert('删除失败');location.href='/Paper/index'</script>");
            }
        }
        public ActionResult Delete(int id)
        {
            if (id != 0)
            {
                testpaper m = new testpaper();
                m.testpaperId = id;
                if (testpaperUI.Delete(m))
                {
                    return Content("<script>alert('删除成功');location.href='/HomePage/index'</script>");
                }
                else
                {
                    return Content("<script>alert('删除失败')</script>");
                }
            }
            else
            {
                return Content("<script>alert('ID不能为空');</script>");
            }
        }
        /// <summary>
        /// 添加试卷
        /// </summary>
        /// <param name="shijuan"></param>
        /// <param name="beizhu"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult shijuan(string shijuan, string beizhu)
        {
            if (!string.IsNullOrWhiteSpace(shijuan))
            {
                testpaper m = new testpaper();
                m.testpaperName = shijuan;
                m.testpaperBeiZhu = beizhu;
                if (testpaperUI.Add(m))
                {
                    return Json(1);
                }
                else
                {
                    return Json(0);
                }
            }
            else
            {
                return Json(-1);
            }
        }
        [HttpPost]
        public ActionResult xiugaishijuanName(int id)
        {
            return View();
        }
    }
}